package com.component;

import com.component.CandidatesProxy.MethodInvoker;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author fanxiao  2022/3/31
 * @since 1.0.0
 */
public class CandidatesProxyFactory<T> {

	private Class<T> serviceInterface;

	private Map<Method, MethodInvoker> methodCache = new ConcurrentHashMap<>();

	public CandidatesProxyFactory(Class<T> serviceInterface) {
		this.serviceInterface = serviceInterface;
	}

	public Class<T> getServiceInterface() {
		return serviceInterface;
	}


	public Map<Method, MethodInvoker> getMethodCache() {
		return methodCache;
	}

	private T newInstance(CandidatesProxy<T> candidatesProxy) {
		return (T) Proxy.newProxyInstance(serviceInterface.getClassLoader(), new Class[]{serviceInterface}, candidatesProxy);
	}

	public T newInstance() {
		CandidatesProxy candidatesProxy = new CandidatesProxy(serviceInterface, methodCache);
		return (T) newInstance(candidatesProxy);
	}

}